package compiler.terminals.expression;



import howl.types.Datum;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;

import howl.model.PMF;
import compiler.TokenReader;


public class Statement extends Node {

	private String condition = "";

	private String querystr = "";

	public Statement(TokenReader condition) {
		super();
		this.setup(condition);
	}
	
	public Statement(String[] str) {
		for (int i = 0; i < 3; i++) {
			this.condition = this.condition + " " + str[i];
		}
		
		this.querystr = "select from howl.types.Datum where GrandParentID == GPID && "
			+ " name == '" + str[0] + "' && " +" Value "
			+ str[1] + " " + str[2];
	}

	private void setup(TokenReader condition) {
		String[] tmp = new String[3];
		for (int i = 0; i < 3; i++) {
			tmp[i] = condition.consumeToken();
			this.condition = this.condition + " " + tmp[i];
		}

		this.querystr = "select from howl.types.Datum where GrandParentID == GPID && "
				+ " name == '" + tmp[0] + "' && " +" Value "
				+ tmp[1] + " " + tmp[2];
		
		//this.querystr = "select from howl.parser.types.Datum where ParentID == criteria && value > GPID";
	}

	@Override
	public List<Datum> evaluate(Long ID) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Query query = pm.newQuery(this.querystr);
		query.declareParameters("Long GPID");
		query.declareImports("import howl.parser.types.Datum; import java.lang.Double");
		System.out.println(query.toString());	
		return (List<Datum>) query.execute(ID);
		//return (List<Datum>) query.execute(24,2);
	}

	@Override
	public String toString() {
		return condition;
	}

}
